<?php

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap {

    protected function _initAutoload() {

        #      $fc = Zend_Controller_Front::getInstance();
        #      $fc->registerPlugin(new Plugin_Noroute());
#require_once 'Zend/Loader/Autoloader.php';
#$loader = Zend_Loader_Autoloader::getInstance();
#$loader->registerNamespace('JHUB1_');
#$loader = Zend_Loader_Autoloader::getInstance();
#$loader->registerNamespace('Buyers_');
#        $modelLoader = new Zend_Application_Module_Autoloader(array(
        #                       'namespace' => 'JHUB1_',
        #                      'basePath' => APPLICATION_PATH));
        #     return $modelLoader;
        #   $application = new Zend_Application(APPLICATION_ENV, array(
        #        'pluginPaths' => array(
        #            'My_Resource' => APPLICATION_PATH . '/resources/',
        #        ),
        #         'resources' => array(
        #            'FrontController' => array(
        #               'controllerDirectory' => APPLICATION_PATH . '/controllers',
        #          ),
        #      ),
        #  ));
        #return $application;
    }

    protected function _initDateTimeLocalization() {
        
    }

    protected function _initLogging() {
        $this->bootstrap('frontController');
        $logger = new Zend_Log();
        $writer = 'production' == $this->getEnvironment() ?
                new Zend_Log_Writer_Stream(APPLICATION_PATH . '/../logs/app.log') :
                new Zend_Log_Writer_Firebug();
        $logger->addWriter($writer);

        if ('production' == $this->getEnvironment()) {
            $filter = new Zend_Log_Filter_Priority(Zend_Log::CRIT);
            $logger->addFilter($filter);
        }
        $this->_logger = $logger;
        Zend_Registry::set('log', $logger);
    }

    protected function _initTranslations() {
        $this->bootstrap('layout');
        $layout = $this->getResource('layout');
        $view = $layout->getView();

        $configpath = APPLICATION_PATH . '/configs/system.ini';
        $config = new Zend_Config_Ini($configpath, "datetime");
        Zend_Registry::set('dateTimeLocalization', $config);
        date_default_timezone_set($config->timezone);

        $translate = new Zend_Translate('gettext',
                        APPLICATION_PATH . '/languages',
                        null,
                        array('scan' => Zend_Translate::LOCALE_DIRECTORY));
        if ($config->localization != "") {
            $locale = new Zend_Locale($config->localization);
            $requestedLang = $config->localization;
            $this->_logger->info("Bootstrap Requested language from config: " . $requestedLang);
        } else {
            $locale = new Zend_Locale(Zend_Locale::BROWSER);
            $requestedLang = key($locale->getBrowser());
            $this->_logger->info("Bootstrap Requested language from browser: " . $requestedLang);
        }
        #     if(in_array($requestedLang, $translate->getList())) {
        $selectedLanguage = $requestedLang;
        $selected = "from locale";
        #     } else {
        #         $selectedLanguage = $config->defaultlanguage;
        #         $selected = "by default";
        #     }
     #   try {    
            $translate->setLocale($selectedLanguage);
     #   } catch (Exception $e) {
      #      echo $e;
      #  }
        
        Zend_Registry::set('Locale', $locale);
        $this->_logger->info("Bootstrap Selected $selected language: " . $selectedLanguage);
        $view->translate = $translate;
    }

    protected function _initSendIdentityToLayout() {
        $auth = Zend_Auth::getInstance();
        #Zend_Auth::getInstance()->getStorage()->read()->role)      
        if ($auth->hasIdentity()) {
            Zend_Layout::getMvcInstance()->assign('hasIdentity', $auth->hasIdentity());
            $authStorage = $auth->getStorage();
            $identity = $authStorage->read();
            Zend_Layout::getMvcInstance()->assign('userName', $identity->name);
            Zend_Layout::getMvcInstance()->assign('userSurname', $identity->surname);
            Zend_Registry::set('role', $identity->role);
        } else {
            Zend_Registry::set('role', 'guest');
        }

        $this->_acl = new Application_Model_CheckAcl;
        $this->_auth = Zend_Auth::getInstance();

        $fc = Zend_Controller_Front::getInstance();
        $fc->registerPlugin(new Application_Plugin_AccessCheck($this->_acl));
    }

    protected function _initSession() {
        $this->_logger->info('Bootstrap ' . __METHOD__);
        Zend_Session::start();
    }

    protected function _initDbProfiler() {
        if ('production' !== $this->getEnvironment()) {
            $this->bootstrap('db');
            $profiler = new Zend_Db_Profiler_Firebug('All DB Queries');
            $profiler->setEnabled(true);
            $this->getPluginResource('db')
                    ->getDbAdapter()
                    ->setProfiler($profiler);
        }
    }

    protected function _initCusNamespace() {

#		$autoLoader = Zend_Loader_Autoloader::getInstance();
#		$autoLoader->registerNamespace('JHUB1_')
#			   ->setFallbackAutoloader(false)
#			   ->suppressNotFoundWarnings(false); 
#		return $autoLoader;
    }

    function _initViewHelpers() {
        $this->bootstrap('layout');
        $layout = $this->getResource('layout');
        $view = $layout->getView();

        $view->addHelperPath(APPLICATION_PATH . '/views/helpers', 'Jhub1_View_Helper_');

        $view->doctype('HTML4_STRICT');
        $view->headMeta()->appendHttpEquiv('Content-type', 'text/html;charset=utf-8')
                ->appendName('description', 'Using view helpers in Zend_view');

        $view->headTitle()->setSeparator(' - ')
                ->headTitle($view->translate->_('Configuration Panel'));

        $navContainerConfig = new Zend_Config_Xml(APPLICATION_PATH . '/configs/main_navigation.xml', 'nav');
        $navContainer = new Zend_Navigation($navContainerConfig);
        #http://stackoverflow.com/questions/1243697/getting-zend-navigation-menu-to-work-with-jquerys-fisheye/1255289#1255289              
        #      $view->navigation($navContainer);
        $view->navigation($navContainer)->setAcl($this->_acl)->setRole(Zend_Registry::get('role'));

        #     $navContainerConfig = new Zend_Config_Xml(APPLICATION_PATH . '/configs/navigation.xml', 'nav');
#		$navContainer = new Zend_Navigation($navContainerConfig);
#		
#		$view->navigation($navContainer)->setAcl($this->_acl)->setRole(Zend_Registry::get('role'));
    }

    /*
      protected function _initJQueryViewHelpers() {
      $this->bootstrap('layout');
      $layout = $this->getResource('layout');
      $view = $layout->getView();
      $viewRenderer = new Zend_Controller_Action_Helper_ViewRenderer();

      $view->addHelperPath('ZendX/JQuery/View/', 'ZendX_JQuery_View');
      $viewRenderer->setView($view);
      Zend_Controller_Action_HelperBroker::addHelper($viewRenderer);
      }
     */
}

/*

ob_start();
var_dump($GLOBALS);
$data = ob_get_clean();
$fp = fopen("textfile.txt", "w");
fwrite($fp, $data);
fclose($fp); 

*/